package com.tongji.showroom.view
{
	import com.adobe.images.JPGEncoder;
	import com.adobe.serialization.json.JSON;
	import com.tongji.showroom.events.AddImageEvent;
	import com.tongji.showroom.events.AddUserSceneEvent;
	import com.tongji.showroom.events.ChangePropertyEvent;
	import com.tongji.showroom.events.DisplayPictureEvent;
	import com.tongji.showroom.events.FilterEvent;
	import com.tongji.showroom.events.HideFurnitureEvent;
	import com.tongji.showroom.events.ListItemMouseEvent;
	import com.tongji.showroom.events.ShowRoomEvent;
	import com.tongji.showroom.model.FurnitureModel;
	import com.tongji.showroom.model.PictureModel;
	import com.tongji.showroom.model.SearchSceneModel;
	import com.tongji.showroom.model.UserModel;
	import com.tongji.showroom.model.UserSceneModel;
	import com.tongji.showroom.utils.ConstVariableInventory;
	import com.tongji.showroom.utils.DragDropManager;
	import com.tongji.showroom.utils.PopUpAdmin;
	import com.tongji.showroom.utils.Showroom;
	import com.tongji.showroom.view.titleWindows.FilterTitleWindow;
	import com.tongji.showroom.view.titleWindows.SingleFurnitureTitleWindow;
	import com.tongji.showroom.view.titleWindows.SingleFurnitureTitleWindowMediator;
	import com.tongji.showroom.view.twoD.AddBitmap;
	import com.tongji.showroom.view.twoD.Furniture;
	import com.tongji.showroom.view.twoD.OldDistortSprite;
	import com.tongji.showroom.vo.ObjectTranslator;
	import com.tongji.showroom.vo.SavePicture;
	import com.tongji.showroom.vo.SaveScene;
	import com.tongji.showroom.vo.SceneAngle;
	import com.tongji.showroom.vo.SceneInfo;
	import com.tongji.showroom.vo.SceneList;
	import com.tongji.showroom.vo.UserAngle;
	
	import flash.display.*;
	import flash.errors.IllegalOperationError;
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.ui.Mouse;
	import flash.utils.ByteArray;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.BitmapAsset;
	import mx.core.DragSource;
	import mx.core.IUIComponent;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	import mx.states.RemoveChild;
	import mx.utils.Base64Encoder;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.DataGroup;
	import spark.components.Image;
	import spark.components.TitleWindow;
	
	/**
	 * 
	 * @author chenzhongyuan
	 * 2011-7-27
	 */
	
	public class SecondStepEditSceneMediator extends Mediator
	{
		[Inject]
		public var view:SecondStepEditSceneComp;

		[Inject]
		public var pictureModel:PictureModel;
		
		[Inject]
		public var saveModel:UserSceneModel;
		
		[Inject]
		public var sceneModel:SearchSceneModel;
		
		private var sceneAngle:Object = new Object();
		private var backGroundBitmap:AddBitmap;
		private var myDragSource:DragSource;
		private var singleFurnitureTitleWindow:SingleFurnitureTitleWindow = new SingleFurnitureTitleWindow();
		private var dragBitmap:AddBitmap;
		private var pixelVectorH:Vector.<Number> = new Vector.<Number>();//存储H.S.V值
		private var pixelVectorS:Vector.<Number> = new Vector.<Number>();
		private var pixelVectorV:Vector.<Number> = new Vector.<Number>();
		private var finalR:uint = 0;//最终的R.G.B值
		private var finalG:uint = 0;
		private var finalB:uint = 0;
		private var startX:int;
		private var startY:int;
		private var endX:int;
		private var endY:int;
		private var _bitmap:BitmapData;
		private var wholeScene:Object = new Object(); //整个场景，Object类型，包含三个defaultScene
		public var selectedScene:Object = new Object(); //某个场景，Object类型，45度或者90度或者135度
		private var selectedAngle:int = 0; //所选中的角度，int类型

		public var flagUp1:int = 5;
		public var flagBelow1:int = 0;
		
		public var flagUp:int = 3;	
		public var flagBelow:int = 0;
			
		[Bindable]
		public var SceneInfoList:ArrayCollection ;
		public var filterTitleWin:FilterTitleWindow;
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		private var user45:UserAngle;
		private var user135:UserAngle;
		private var user90:UserAngle;
		[Inject]
		public var userModel:UserModel;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.uploadPictureList.dataGroup, "dataProvider", pictureModel, "tempArray");
			BindingUtils.bindProperty(view.briefMessageList.dataGroup, "dataProvider", sceneModel, "selectSceneArray");			
			
			sceneModel.tempSceneArray = new ArrayCollection();
			addViewListener(ShowRoomEvent.SHOW_ROOM, showRoomHandler);
			this.addContextListener(ShowRoomEvent.SHOW_ROOM, showRoomHandler);
			
			//打开该页面默认展示第一个场景的第一个角度
			if (sceneModel.selectSceneArray.length > 0)
			{
				dispatch(new ShowRoomEvent(ShowRoomEvent.SHOW_ROOM, sceneModel.selectSceneArray[0]));
				view.briefMessageList.selectedIndex = 0;
			}
			
			eventMap.mapListener(view.ScreenShotBtn, MouseEvent.CLICK, screenShotHandler);
			eventMap.mapListener(view.smallScreenBtn, MouseEvent.CLICK, smallScreenHandler);
			eventMap.mapListener(view, ListItemMouseEvent.ITEM_MOUSE_DOWN, itemMouseDownHandler, ListItemMouseEvent);
			eventMap.mapListener(view.leftUpBtn, MouseEvent.CLICK, leftUpMove);
			eventMap.mapListener(view.rightUpBtn, MouseEvent.CLICK, rightUpMove);
			eventMap.mapListener(view.leftDownBtn, MouseEvent.CLICK, leftMove);
			eventMap.mapListener(view.rightDownBtn, MouseEvent.CLICK, rightMove);
			eventMap.mapListener(view.saveBtn, MouseEvent.CLICK, saveSceneHandler);
			addContextListener(AddImageEvent.UPDATE_IMAGE, addUserSceneHandler, AddImageEvent);
		}
		
		private function addUserSceneHandler(event:AddImageEvent):void
		{
			var sceneStr:String = saveModel.save(view.briefMessageList.selectedItem, userModel.userID);
			dispatch(new AddUserSceneEvent(AddUserSceneEvent.ADD_USER_SCENE, sceneStr));
		}
		
		private function smallScreenHandler(event:MouseEvent):void
		{
			
			var fur:Furniture = view.roomDisplay.getChildAt(4) as Furniture;
			
			var map:BitmapData = fur.distortSprite.bitmap.bitmapData;
			var boundingBox:Rectangle = fur.getRect(fur); 
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height, true, 0x00000000);
			bitmapData.draw(fur.distortSprite, matrix); 
			var jpg:PNGEncoder = new PNGEncoder(); 
			var jpgByteArray:ByteArray = jpg.encode(bitmapData); 
			var fr:FileReference = new FileReference(); 
			fr.save(jpgByteArray, 'export.png');
		}
		
		/** 截图
		 * @autor jiarong
		 * @param event
		 *
		 */		
		private function screenShotHandler(event:MouseEvent):void
		{
			var boundingBox:Rectangle = view.borderContainer.getRect(view.borderContainer);
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height);
			bitmapData.draw(view.borderContainer, matrix); 
			var jpg:JPEGEncoder = new JPEGEncoder(); 
			var jpgByteArray:ByteArray = jpg.encodeByteArray(bitmapData.getPixels(boundingBox), boundingBox.width, boundingBox.height); 
			var fr:FileReference = new FileReference(); 
			fr.save(jpgByteArray, 'export.jpg');
		}
		
		/**
		 * @author caddy 
		 * @param event
		 * 
		 */		
		private function saveSceneHandler(event:MouseEvent):void
		{
			/*var userScene:Object = view.briefMessageList.selectedItem;
			saveModel.saveScene(userScene);	
			var boundingBox:Rectangle = view.borderContainer.getRect(view.borderContainer);
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height);
			bitmapData.draw(view.borderContainer, matrix);
			var encoder:mx.graphics.codec.JPEGEncoder = new mx.graphics.codec.JPEGEncoder();
			var byte:ByteArray = encoder.encode(bitmapData);
			var Base64:Base64Encoder = new Base64Encoder();
			Base64.encodeBytes(byte);
			var image:String = Base64.toString();
			var object:Object = new Object();
			object.uId = userModel.userID;
			object.ImgUrl = image;
			object.sid = userScene.sid;
			var str:String = JSON.encode(object);
			var userAngleCount:int = saveModel.userAngles.length;
			var systemAngelCount:int = userScene.SceneAngleList.length;
			var childCount:int = view.roomDisplay.numChildren;
			var furniture:Furniture; 
			for (var i:int = 0; i < childCount; i++)
			{
				if (view.roomDisplay.getChildAt(i) is Furniture)
				{
					furniture = view.roomDisplay.getChildAt(i) as Furniture;
					saveModel.currentPosition = new Point(furniture.x, furniture.y);
				}
			}*/
			switch(selectedScene.Jdid)
			{
				case 31:
					user45 = new UserAngle();
					//user45.furniture = furniture;
					user45.sceneAngleID = selectedScene.Asid;
					user45.angleID = 31;
					user45.img = getImg();
					break;
				case 30:
					user135 = new UserAngle();
					//user135.furniture = furniture;
					user135.sceneAngleID = selectedScene.Asid;
					user135.angleID = 30;
					user135.img = getImg();
					break;
				case 35:
					user90 = new UserAngle();
					//user90.furniture = furniture;
					user90.sceneAngleID = selectedScene.Asid;
					user90.angleID = 35;
					user90.img = getImg();
					break;
			}
			if (user45 == null || user135 == null || user90 == null)
			{
				Alert.show("您的角度还未调整完");
			}
			else
			{
				saveModel.userAngles = new Array();
				saveModel.userAngles.push(user135);
				saveModel.userAngles.push(user45);
				saveModel.userAngles.push(user90);
				
				var userImages:ArrayCollection = new ArrayCollection();
				userImages.addItem(user135.img);
				userImages.addItem(user45.img);
				userImages.addItem(user90.img);
				
				dispatch(new AddImageEvent(AddImageEvent.ADD_IMAGES, userImages));
			}
		}
		private function getImg():String
		{
			var boundingBox:Rectangle = view.borderContainer.getRect(view.borderContainer);
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height);
			bitmapData.draw(view.borderContainer, matrix);

			var encoder:JPGEncoder = new JPGEncoder();
			var byte:ByteArray = encoder.encode(bitmapData);
			var Base64:Base64Encoder = new Base64Encoder();
			Base64.encodeBytes(byte);
			var image:String = Base64.toString();
			return image;			
		}
		
		private function itemMouseDownHandler(event:ListItemMouseEvent):void
		{
			furnitureModel.furniture = new Furniture(event.target.data, event.content);
			DragDropManager.startDragFurniture(view, event.myEvent, new Furniture(event.target.data, event.content));
		}
		
		/*public function rollBackPicture():void
		{
			var tempPic:AddBitmap;
			for(var i:int = 0;i<saveModel.saveSceneArray.length;i++)
			{
				for(var j:int = 0;j<saveModel.saveSceneArray[i].savePicturesArray.length;j++)
				{
					var savePic:SavePicture = saveModel.saveSceneArray[i].savePicturesArray[j];
					if(savePic != null && savePic.sceneAngleID == this.sceneAngle.Asid)
					{
						for each(var k:Object in pictureModel.tempArray)
						{
							if(k.pImgUrl == savePic.savePictureSource)
							{
								tempPic = new AddBitmap(savePic.savePictureSource,savePic.point.x,savePic.point.y);
								break;
							}
						}
						
						var ds:DistortSprite = new DistortSprite(tempPic.addBitmapData,true);
						view.roomDisplay.addChild(ds);
						ds.x = savePic.point.x;
						ds.y = savePic.point.y;
						ds.scaleX = ConstVariableInventory.RECT_WIDTH/ConstVariableInventory.PICTURE_WIDTH;
						ds.scaleY = ConstVariableInventory.RECT_HEIGHT/ConstVariableInventory.PICTURE_HEIGHT;
					}
				}
			}
		}*/
	
		public function showRoomHandler(event:ShowRoomEvent):void
		{		
			wholeScene = event.data;
			selectedScene = event.data.SceneAngleList[0];
			initScene(0);
			var showroom:Showroom = new Showroom(view.roomDisplay, event.data, 0);
	
			//设置默认场景
			/*if(!saveModel.enable)//frank
			{
				defaultAngle = event.data.SceneAngleList[0];
			}
			else
			{
				for(var i:int = 0;i < saveModel.saveSceneArray.length && view.briefMessageList.selectedItem != saveModel.saveSceneArray[i].saveScene; i++)//frank
				{
				}
				if(i == saveModel.saveSceneArray.length)
					defaultAngle = event.data.SceneAngleList[0];
				else
				{
					defaultAngle = saveModel.saveSceneArray[i].saveScene.SceneAngleList[0];
				}
			}
			sceneAngle = defaultAngle;
			backGroundBitmap = new AddBitmap(sceneAngle.Bgurl, ConstVariableInventory.PICTURE_WIDTH / 2, ConstVariableInventory.PICTURE_HEIGHT / 2)
			view.roomDisplay.addChild(backGroundBitmap);
					
			sceneInfoList = new ArrayCollection();
			
			var maxDepth:int = 0;
			
			for each(var sceneInfo1:Object in defaultAngle.SceneInfoList)
			{
				if (sceneInfo1.depth > maxDepth)
					maxDepth = sceneInfo1.depth;
			}
			
			var i:int = maxDepth;
			
			while (i > 0)
			{
				for each(var sceneInfo:Object in defaultAngle.SceneInfoList)
				{				
					if (sceneInfo.depth == i)
					{
						var addBitmap:AddBitmap = new AddBitmap(sceneInfo.ProductUrl, sceneInfo.xAxis, sceneInfo.yAxis);	
						view.roomDisplay.addChild(addBitmap);
						sceneInfo.sceneInfoBitmap = addBitmap;
						if(sceneInfo.sceneInfoVisible == null)
							sceneInfo.sceneInfoVisible = true;
						addBitmap.visible = sceneInfo.sceneInfoVisible;
						ObjectTranslator.objectToInstance(sceneInfo, SceneInfo);
						sceneInfoList.addItem(sceneInfo);
						i--;
					}	
				}	
			}*/
		}
		
		public function initScene(num:int):void
		{			
			//对展示厅初始化
			view.uploadPictureList.enabled = true;
			view.angleComp.angle45Btn.enabled = false;
			view.angleComp.angle90Btn.enabled = false;
			view.angleComp.angle135Btn.enabled = false;
			view.uploadPictureList.selectedIndex = -1;	
			PopUpManager.removePopUp(singleFurnitureTitleWindow);
			mediatorMap.removeMediatorByView(singleFurnitureTitleWindow);
			furnitureModel.furniturCount = 0;
			//按场景角度设置场景角度变换按钮的可用性
			for each (var angle:Object in wholeScene.SceneAngleList)
			{
				switch (angle.Jdid)
				{
					case 30://正45°
						view.angleComp.angle45Btn.enabled = true;
						break;
					case 31://负45°
						view.angleComp.angle135Btn.enabled = true;
						break;
					case 35://90°
						view.angleComp.angle90Btn.enabled = true;
						break;
					default:
						break;						
				}
			}
			
			//设置被选中角度按钮状态
			switch (selectedScene.Jdid)
			{
				case 30: view.angleComp.angle45Btn.selected = true;
					break;
				case 31: view.angleComp.angle135Btn.selected = true;
					break;
				case 35: view.angleComp.angle90Btn.selected = true;
					break;
				default: break;
			}
			
			sceneModel.tempSceneArray.removeAll();
			for (var i:int = 0; i < selectedScene.SceneInfoList.length; i++)
			{
				sceneModel.tempSceneArray.addItem(selectedScene.SceneInfoList[i]);
			}
			
			view.hideFurnitureList.visible = true;
			BindingUtils.bindProperty(view.hideFurnitureList, "dataProvider", sceneModel, "tempSceneArray");
			addViewListener(HideFurnitureEvent.HIDE_A_FURNITURE, hide);
			eventMap.mapListener(view.angleComp.angle45Btn, MouseEvent.CLICK, changeAngle45Handler);
			eventMap.mapListener(view.angleComp.angle90Btn, MouseEvent.CLICK, changeAngle90Handler);
			eventMap.mapListener(view.angleComp.angle135Btn, MouseEvent.CLICK, changeAngle135Handler);
		}
		
		public function changeAngle45Handler(event:MouseEvent):void
		{
			selectedAngle = 30;
			/*var childCount:int = view.roomDisplay.numChildren;
			var furniturePoints:Array = new Array();
			var furniture:Furniture; 
			for (var i:int = 0; i < childCount; i++)
			{
				if (view.roomDisplay.getChildAt(i) is Furniture)
				{
					furniture = view.roomDisplay.getChildAt(i) as Furniture;
					saveModel.currentPosition = new Point(furniture.x, furniture.y);
				}
			}
			var userAngle:UserAngle = new UserAngle();
			userAngle.furniture = furniture;
			userAngle.sceneAngleID = selectedScene.Asid;
			userAngle.angleID = 30;
			userAngle.img = getImg();
			saveModel.userAngles.push(userAngle);*/
			wholeScene = view.briefMessageList.selectedItem;
			changeAngle();
		}
		
		public function changeAngle90Handler(event:MouseEvent):void
		{
			/*var childCount:int = view.roomDisplay.numChildren;
			var furniture:Furniture; 
			for (var i:int = 0; i < childCount; i++)
			{
				if (view.roomDisplay.getChildAt(i) is Furniture)
				{
					furniture = view.roomDisplay.getChildAt(i) as Furniture;
					saveModel.currentPosition = new Point(furniture.x, furniture.y);
				}
			}
			var userAngle:UserAngle = new UserAngle();
			userAngle.furniture = furniture;
			userAngle.sceneAngleID = selectedScene.Asid;
			userAngle.angleID = 35;
			userAngle.img = getImg();
			saveModel.userAngles.push(userAngle);*/
			selectedAngle = 35;
			wholeScene = view.briefMessageList.selectedItem;
			changeAngle();
		}
		
		public function changeAngle135Handler(event:MouseEvent):void
		{
			/*var childCount:int = view.roomDisplay.numChildren;
			var furniture:Furniture; 
			for (var i:int = 0; i < childCount; i++)
			{
				if (view.roomDisplay.getChildAt(i) is Furniture)
				{
					furniture = view.roomDisplay.getChildAt(i) as Furniture;
					saveModel.currentPosition = new Point(furniture.x, furniture.y);
				}
			}
			var userAngle:UserAngle = new UserAngle();
			userAngle.furniture = furniture;
			userAngle.sceneAngleID = selectedScene.Asid;
			userAngle.angleID = 31;
			userAngle.img = getImg();
			saveModel.userAngles.push(userAngle);*/
			selectedAngle = 31;
			wholeScene = view.briefMessageList.selectedItem;
			changeAngle();
		}
		
		public function changeAngle():void
		{
			var childCount:int = view.roomDisplay.numChildren;
			var furniture:Furniture; 
			for (var i:int = 0; i < childCount; i++)
			{
				if (view.roomDisplay.getChildAt(i) is Furniture)
				{
					furniture = view.roomDisplay.getChildAt(i) as Furniture;
					saveModel.currentPosition = new Point(furniture.x, furniture.y);
				}
			}
			switch(selectedScene.Jdid)
			{
				case 31:
					user45 = new UserAngle();
					user45.furniture = furniture;
					user45.sceneAngleID = selectedScene.Asid;
					user45.angleID = 31;
					user45.img = getImg();
					break;
				case 30:
					user135 = new UserAngle();
					user135.furniture = furniture;
					user135.sceneAngleID = selectedScene.Asid;
					user135.angleID = 30;
					user135.img = getImg();
					break;
				case 35:
					user90 = new UserAngle();
					user90.furniture = furniture;
					user90.sceneAngleID = selectedScene.Asid;
					user90.angleID = 35;
					user90.img = getImg();
					break;
			}			
			
			eventMap.unmapListener(view.angleComp.angle45Btn, MouseEvent.CLICK, changeAngle45Handler);
			eventMap.unmapListener(view.angleComp.angle90Btn, MouseEvent.CLICK, changeAngle90Handler);
			eventMap.unmapListener(view.angleComp.angle135Btn, MouseEvent.CLICK, changeAngle135Handler);

			for each (var subScene:Object in wholeScene.SceneAngleList)
			{
				if (subScene.Jdid == selectedAngle)
				{
					selectedScene = subScene;
					break;
				}
			}
			
			var showRoom2:Showroom = new Showroom(view.roomDisplay, wholeScene, selectedAngle);
		    initScene(selectedAngle);	
		}
		
		
		public function hide(event:HideFurnitureEvent):void
		{	
			event.furniture.sceneInfoBitmap.visible = !event.furniture.sceneInfoBitmap.visible;
		}
		/*public function changeAngle(event:MouseEvent):void
		{
			initScene(selectedAngle);
			
			view.uploadPictureList.selectedIndex = -1;
			view.uploadPictureList.enabled = true;
			PopUpManager.removePopUp(singleFurnitureTitleWindow);
			mediatorMap.removeMediatorByView(singleFurnitureTitleWindow);
			sceneInfoList = new ArrayCollection();

			
			if (view.angleComp.angle45Btn.selected == true)
			{
				sceneAngle = angle45;
			}				
			if (view.angleComp.angle90Btn.selected == true)
			{
				sceneAngle = angle90;
			}
			if (view.angleComp.angle135Btn.selected == true)
			{
				sceneAngle = angle135;
			}
			
			view.roomDisplay.image.source = sceneAngle.Bgurl;
			var backGroundBitmap:AddBitmap = new AddBitmap(sceneAngle.Bgurl, ConstVariableInventory.PICTURE_WIDTH / 2, ConstVariableInventory.PICTURE_HEIGHT / 2);
			view.roomDisplay.addChild(backGroundBitmap);
			
			var maxDepth:int = 0;
			
			if(saveModel.enable == false)//frank
			{
				for each(var sceneInfo1:Object in sceneAngle.SceneInfoList)
				{
					if (sceneInfo1.depth > maxDepth)
						maxDepth = sceneInfo1.depth;
				}
				
				var i:int = maxDepth;
				
				while (i > 0)
				{
					for each(var sceneInfo:Object in sceneAngle.SceneInfoList)
					{				
						if (sceneInfo.depth == i)
						{
							var addBitmap:AddBitmap = new AddBitmap(sceneInfo.ProductUrl, sceneInfo.xAxis, sceneInfo.yAxis);	
							view.roomDisplay.addChild(addBitmap);
							sceneInfo.sceneInfoBitmap = addBitmap;
							sceneInfo.sceneInfoVisible = true;
							ObjectTranslator.objectToInstance(sceneInfo, SceneInfo);
							sceneInfoList.addItem(sceneInfo);
							i--;
						}	
					}	
				}
				BindingUtils.bindProperty(view.hideFurnitureList, "dataProvider", this, "sceneInfoList");
			}
			else
			{
				var saved:Boolean = false;
				var index:int = 0;
				while(view.briefMessageList.selectedItem != saveModel.saveSceneArray[index].saveScene)
				{
					index ++;
				}
				if(index != saveModel.saveSceneArray.length)
					saved = true;
				if(saved)
				{
					var i:int = 0;
					while(sceneAngle.Jdid != saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].Jdid)
					{
						i++;
					}
					for (var j:int = 0;j < saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].SceneInfoList.length;j++)
					{
						var sceneInfo:Object = saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].SceneInfoList[j];
						var addBitmap:AddBitmap = new AddBitmap(sceneInfo.ProductUrl, sceneInfo.xAxis, sceneInfo.yAxis);	
						view.roomDisplay.addChild(addBitmap);
						
						sceneInfo.sceneInfoBitmap = addBitmap;
						//if(sceneInfo.sceneInfoVisible)
						//sceneInfo.sceneInfoVisible = true;
						ObjectTranslator.objectToInstance(sceneInfo, SceneInfo);
						sceneInfoList.addItem(sceneInfo);
						if(sceneInfo.sceneInfoVisible != null)
						{
							addBitmap.visible = sceneInfo.sceneInfoVisible;
						}
						else
						{
							addBitmap.visible = true;
							sceneInfo.sceneInfoVisible = true;
						}
					}
					rollBackPicture(); 
					tempSceneInfoList.removeAll();
					
					for (var k:int = 0;k < saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].SceneInfoList.length;k++)
					{
						tempSceneInfoList.addItem(saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].SceneInfoList[k] as Object);
					}
					//ObjectTranslator.objectToInstance(saveModel.saveSceneArray[index].saveScene.SceneAngleList[i], SceneAngle);
					//this.sceneInfoList = saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].SceneInfoList;
					BindingUtils.bindProperty(view.hideFurnitureList, "dataProvider", this, "tempSceneInfoList");
					//view.hideFurnitureList.dataProvider = saveModel.saveSceneArray[index].saveScene.SceneAngleList[i].SceneInfoList as ArrayCollection;
					
				}
				
			}
		}*/

		
		/*
		public function dropHandler(event:DragEvent):void//与r122同步
		{	
		var rect:Rectangle = dragBitmap.addBitmapData.getColorBoundsRect(0xff000000, 0x00000000, true);
		_bitmap = new BitmapData(rect.width, rect.height);
		_bitmap.copyPixels(dragBitmap.addBitmapData, rect, new Point(0 ,0));
		
		distortSprite = new DistortSprite(_bitmap, true);
		
		//缩放
		distortSprite.scaleX = rectWidth / oriWidth;
		distortSprite.scaleY = rectHeight / oriHeight;
		
		// 将家具拖到舞台时首次检查是否出界
		var up:Boolean = false;
		var down:Boolean = false;		
		var left:Boolean = false;
		var right:Boolean = false;
		
		//存放背景RGB值，用在色彩平衡的时候
		var array:Array = new Array();
		
		//四周是否超界
		up = event.localY < distortSprite.height / 2;
		down = (event.localY + distortSprite.height / 2) > rectHeight;
		left = event.localX < distortSprite.width / 2;
		right = (event.localX + distortSprite.width / 2) > rectWidth;
		
		if (up || down || left || right)
		trace("不能拖到此处！");
		else
		{
		view.roomDisplay.addChild(distortSprite);
		
		distortSprite.x = event.localX - int(distortSprite.width / 2);
		distortSprite.y = event.localY - int(distortSprite.height / 2);	
		view.roomDisplay.addEventListener(MouseEvent.MOUSE_UP, upHandler);	
		eventMap.mapListener(view.roomDisplay, MouseEvent.MOUSE_UP, upHandler);
		
		view.uploadPictureList.enabled = false;
		first = true;
		
		startX = distortSprite.x / distortSprite.scaleX;
		startY = distortSprite.y / distortSprite.scaleY;
		endX = distortSprite.width / distortSprite.scaleX + startX;
		endY = distortSprite.height / distortSprite.scaleY + startY;
		
		//存储背景图片
		var btmData:BitmapData = backGroundBitmap.addBitmapData;
		
		//存储所拖动图片周围环状部分的RGB数据
		for (var i:int = startY - 100; i < startY; i++)
		for (var j:int = startX - 100; j < endX + 100; j++)
		array.push(btmData.getPixel(j, i));
		
		for (var ic:int = endY; ic < endY + 100; ic++)
		for (var jc:int = startX - 100; jc < endX + 100; jc++)
		array.push(btmData.getPixel(jc, ic));
		
		for (var id:int = startY; id < endY; id++)
		for (var jd:int = startX - 100; jd < startX; jd++)
		array.push(btmData.getPixel(jd, id));
		
		for (var ie:int = startY; ie < endY; ie++)
		for (var je:int = endX; je < endX + 100; je++)
		array.push(btmData.getPixel(je, ie));
		
		colorBalance(array);
		}
		
		distortSpriteArray.addItem(distortSprite);	
		
		//save(distortSprite);
		
		eventMap.mapListener(view.roomDisplay, DragEvent.DRAG_ENTER, enterHandler);
		eventMap.mapListener(view.roomDisplay, DragEvent.DRAG_DROP, dropHandler);
		
		}
		
		/*private function save(ds:DistortSprite):void//frank
		{
		//var point:Point = ds.localToGlobal(new Point(ds.x, ds.y));
		for(var i:int = 0; i < 8; i ++)
		{
		var tempPoint:Point = new Point();
		tempPoint.x = ds.controlList[i].x;
		tempPoint.y = ds.controlList[i].y;
		saveModel.
		}
		saveModel.referencePoint.x = ds.x;
		saveModel.referencePoint.y = ds.y;			
		saveModel.image = ds.bitmapData;
		}
		private function colorBalance(backarray:Array):void
		{
			//计算平均HSV值
			var sumH:Number = 0;
			var sumS:Number = 0;
			var sumV:Number = 0;
			
			var sumBack:uint = 0;
			var backcolor:uint = 0;
			
			var sumFront:uint = 0;
			var frontcolor:uint = 0;
			
			var frontArray:Array = new Array();
			
			for(var i:int = 0; i < backarray.length; i++)
				sumBack = sumBack + backarray[i];
			
			backcolor = sumBack / backarray.length;
			
			//求背景的SV
			var backSV:Array = getBackSV(backcolor);
			
			//存储所拖动图片每个像素点的HSV值
			for (var ia:int = 0; ia < distortSprite.width; ia++)
				for (var ja:int = 0; ja < distortSprite.height; ja++)
				{
					var color:uint = _bitmap.getPixel(ja, ia);
					if((color >> 24 & 0xFF) != 0)
						frontArray.push(color);
				}
			
			for(var j:uint; j < frontArray.length; j++)
				sumFront = sumFront + frontArray[j];
			
			frontcolor = sumFront / frontArray.length;
			
			var frontHSV:Array = getHsvHandler(frontcolor);
			
			getRGBHandler(frontHSV[0], backSV[0] * 0.25 + frontHSV[1] * 0.75, backSV[1] * 0.25 + frontHSV[2] * 0.75);
			var finalRGB:uint = (uint)(finalR << 16) + (uint)(finalG << 8) + (uint)(finalB);
			
			distortSprite.bitmapData.lock();
			for (var je:int = 0; je < distortSprite.bitmapData.height; je++)
				for (var ie:int = 0; ie < distortSprite.bitmapData.width; ie++)
				{
					distortSprite.bitmapData.setPixel(ie, je, finalRGB);
				}
			distortSprite.bitmapData.unlock();
		}
		
		//将已平均的背景颜色的RGB值转换为SV
		public function getBackSV(color_back:uint):Array
		{
			var sv:Array = new Array(2);
			
			var back_r:Number = (color_back >> 16 & 0xFF) / 255;
			var back_g:Number = (color_back >> 8 & 0xFF) / 255;
			var back_b:Number = (color_back & 0xFF) / 255;
			
			var max_b:Number = back_r < (back_g < back_b ? back_g : back_b) ? back_r : (back_g < back_b ? back_g : back_b);
			var min_b:Number = back_r > (back_g > back_b ? back_g : back_b) ? back_r : (back_g > back_b ? back_g : back_b);
			
			var s_b:Number;
			var v_b:Number = max_b;
			
			if(max_b == min_b)//RGB --> HSV		求背景的HSV中的SV
			{
				s_b = 0;
			}
			else
			{
				s_b = (max_b - min_b) / max_b;
			}
			
			sv[0] = s_b;
			sv[1] = v_b;
			
			return sv;
		}
		
		public function upHandler(event:MouseEvent):void
		{
			if (event.target is UIComponent)  //如果鼠标落在图片外
			{
				if (singleFurnitureTitleWindow)  //隐藏titleWindow
				{
					PopUpManager.removePopUp(singleFurnitureTitleWindow);
					mediatorMap.removeMediatorByView(singleFurnitureTitleWindow);	
				}
				
				for(var i:int = 0; i < distortSpriteArray.length; i++)  //隐藏控制点
				{ 
					distortSpriteArray[i].controlDotBox.visible = false;
				}
			}
			else if (event.target is DistortSprite)  //如果鼠标落在图片非空白处
			{
				for (var j:int = 0; j < distortSpriteArray.length; j++)
				{
					if (event.target == distortSpriteArray[j])
					{
						clicking = j;
						distortSpriteArray[clicking].controlDotBox.visible = true;
						singleFurnitureTitleWindow.name = "singleFurnitureProperty";
						PopUpManager.addPopUp(singleFurnitureTitleWindow, view.parent, false);
						
						singleFurnitureTitleWindow.x = distortSpriteArray[clicking].x + distortSpriteArray[clicking].width + 50;
						singleFurnitureTitleWindow.y = distortSpriteArray[clicking].y + distortSpriteArray[clicking].height + 180;
						mediatorMap.createMediator(singleFurnitureTitleWindow);			
					}
					else
					{
						distortSpriteArray[j].controlDotBox.visible = false;
					}
				}
			}
			else  //如果鼠标落在图片空白处
			{
				for(var k:int = 0; k < distortSpriteArray.length; k++)
				{
					distortSpriteArray[k].controlDotBox.visible = true;
				}
			}	
		}
		*/
		
		/*public function saveSceneHandler(event:MouseEvent):void//frank
		{
		if(!saveModel.enable)
		{
		var saveScene:SaveScene = new SaveScene();
		saveScene.saveScene = view.briefMessageList.selectedItem;
		saveTransPicture(saveScene);
		saveScene.ImgUrl = view.briefMessageList.selectedItem.ImgUrl;
		saveModel.saveSceneArray.addItem(saveScene);
		saveModel.enable = true;
		}
		else
		{
		for (var i:int = 0;i < saveModel.saveSceneArray.length && view.briefMessageList.selectedItem.sid != saveModel.saveSceneArray[i].saveScene.sid; i++)
		{}
		if(i != saveModel.saveSceneArray.length)
		{
		saveModel.saveSceneArray[i].saveScene = view.briefMessageList.selectedItem;
		saveTransPicture(saveModel.saveSceneArray[i].saveScene);				
		}
		else
		{
		var saveScene2:SaveScene = new SaveScene();
		saveScene2.ImgUrl = view.briefMessageList.selectedItem.ImgUrl;
		saveScene2.saveScene = view.briefMessageList.selectedItem;
		saveTransPicture(saveScene2);
		saveModel.saveSceneArray.addItem(saveScene2);
		saveModel.enable = true;
		}
		}
		}*/
		
		/*public function saveTransPicture(saveScene:SaveScene):void//保存变形的图片frank
		{
		var savePicture:SavePicture = new SavePicture();
		for(var i:int = 0; i<8; i++)
		{
		var tempPoint:Point = new Point();
		tempPoint.x = this.distortSprite.controlList[i].x;
		tempPoint.y = this.distortSprite.controlList[i].y;
		savePicture.points.addItem(tempPoint);
		}
		savePicture.sceneAngleID = this.sceneAngle.Asid;
		//savePicture.savePictureBitmapData = this.distortSprite.bmp;
		savePicture.savePictureSource = sceneModel.tempImage.source.toString();
		savePicture.point.x = this.distortSprite.x;
		savePicture.point.y = this.distortSprite.y;
		saveScene.savePicturesArray.addItem(savePicture);
		}*/
		private function leftUpMove(event:MouseEvent):void
		{
			if (flagUp1 <= sceneModel.sceneArray.length - 2)
			{
				flagUp1++;
				flagBelow++;
				view.briefMessageList.ensureIndexIsVisible(flagUp1);
			}
		}
		
		private function rightUpMove(event:MouseEvent):void
		{
			if (flagBelow1 >= 1)
			{
				flagBelow1--;
				flagUp1--;
				view.briefMessageList.ensureIndexIsVisible(flagBelow1);
			}
		}
		
		private function leftMove(event:MouseEvent):void
		{
			if (flagUp <= pictureModel.tempArray.length - 2)
			{
				flagUp++;
				flagBelow++;
				view.uploadPictureList.ensureIndexIsVisible(flagUp);
			}
		}
		
		private function rightMove(event:MouseEvent):void
		{
			if (flagBelow >= 1)
			{
				flagBelow--;
				flagUp--;
				view.uploadPictureList.ensureIndexIsVisible(flagBelow);
			}
		}
	}
}